package protocol;

import java.util.Map;

public class SignedResponse {

    public long id;

    public Map<String, String> state;

    public Map<String, Object> data;

    public boolean hasKey(String key){
        if(data == null || key == null){
            return false;
        }
        return data.containsKey(key);
    }
    
    public Object getObject(String key){
        if(data == null || key == null){
            return null;
        }
        return data.get(key);
    }
    
    public String getString(String key){
        return getString(key, null);
    }
    
    public String getString(String key, String defaultValue){
        Object value = getObject(key);
        if(value == null){
            return defaultValue;
        }
        return value.toString();
    }
    
    public int getInt(String key, int defaultValue){
        Object value = getObject(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Number){
            return ((Number) value).intValue();
        }
        try{
            return Integer.parseInt(value.toString());
        }catch(NumberFormatException e){
            return defaultValue;
        }
    }
    
    public long getLong(String key, long defaultValue){
        Object value = getObject(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Number){
            return ((Number) value).longValue();
        }
        try{
            return Long.parseLong(value.toString());
        }catch(NumberFormatException e){
            return defaultValue;
        }
    }
}
